home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / experti / d7 / GE / GX7ProEnt-112.exe / {app} / DbugIntf.pas next >
Pascal/Delphi Source File  |  2002-01-29  |  5KB  |  193 lines

  1. unit DbugIntf;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Dialogs; // We need "Dialogs" for TMsgDlgType
  7.  
  8. procedure SendBoolean(const Identifier: string; const Value: Boolean);
  9. procedure SendDateTime(const Identifier: string; const Value: TDateTime);
  10. procedure SendDebugEx(const Msg: string; MType: TMsgDlgType);
  11. procedure SendDebug(const Msg: string);
  12. procedure SendDebugClear;
  13. procedure SendInteger(const Identifier: string; const Value: Integer);
  14. procedure SendMethodEnter(const MethodName: string);
  15. procedure SendMethodExit(const MethodName: string);
  16. procedure SendSeparator;
  17. procedure SendDebugFmt(const Msg: string; const Args: array of const);
  18. procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TMsgDlgType);
  19. function StartDebugWin: hWnd;
  20.  
  21. implementation
  22.  
  23. uses
  24.   Messages,
  25.   SysUtils,
  26.   Registry,
  27.   Forms; // We need "Forms" for the Application object
  28.  
  29. threadvar
  30.   MsgPrefix: AnsiString;
  31.  
  32. const
  33.   chrClearCommand = #3;
  34.  
  35. var
  36.   PastFailedAttemptToStartDebugWin: Boolean = False;
  37.  
  38. function StartDebugWin: hWnd;
  39. var
  40.   DebugFilename: string;
  41.   Buf: array[0..MAX_PATH + 1] of Char;
  42.   si: TStartupInfo;
  43.   pi: TProcessInformation;
  44. begin
  45.   MsgPrefix := '';
  46.  
  47.   Result := 0;
  48.   if PastFailedAttemptToStartDebugWin then
  49.     Exit;
  50.  
  51.   with TRegIniFile.Create('\Software\GExperts') do
  52.   try
  53.     DebugFilename := ReadString('Debug', 'FilePath', '');
  54.   finally
  55.     Free;
  56.   end;
  57.  
  58.   if Trim(DebugFileName) = '' then
  59.   begin
  60.     GetModuleFileName(HINSTANCE, Buf, SizeOf(Buf)-1);
  61.     DebugFileName := ExtractFilePath(StrPas(Buf))+'GDebug.exe';
  62.   end;
  63.  
  64.   if (Trim(DebugFilename) = '') or not FileExists(DebugFilename) then
  65.   begin
  66.     PastFailedAttemptToStartDebugWin := True;
  67.     Exit;
  68.   end;
  69.  
  70.   FillChar(si, SizeOf(si), #0);
  71.   si.cb := SizeOf(si);
  72.   si.dwFlags := STARTF_USESHOWWINDOW;
  73.   si.wShowWindow := SW_SHOW;
  74.   if not CreateProcess(PChar(DebugFilename), nil,
  75.                        nil, nil,
  76.                        False, 0, nil, nil,
  77.                        si, pi) then
  78.   begin
  79.     PastFailedAttemptToStartDebugWin := True;
  80.     Exit;
  81.   end;
  82.  
  83.   try
  84.     WaitForInputIdle(pi.hProcess, 3 * 1000); // wait for 3 seconds to get idle
  85.   finally
  86.     CloseHandle(pi.hThread);
  87.     CloseHandle(pi.hProcess);
  88.   end;
  89.  
  90.   Result := FindWindow('TfmDebug', nil);
  91. end;
  92.  
  93. procedure SendDebugEx(const Msg: string; MType: TMsgDlgType);
  94. var
  95.   CDS: TCopyDataStruct;
  96.   DebugWin: hWnd;
  97.   MessageString: string;
  98. {$IFDEF LINUX}
  99. const
  100.   MTypeStr: array[TMsgDlgType] of string =
  101.     ('Warning: ', 'Error: ', 'Information: ', 'Confirmation: ', 'Custom: ');
  102. {$ENDIF LINUX}
  103. begin
  104. {$IFDEF LINUX}
  105.   Writeln('GX: ' + MTypeStr[MType] + Msg);
  106. {$ENDIF LINUX}
  107. {$IFNDEF LINUX}
  108.   DebugWin := FindWindow('TfmDebug', nil);
  109.  
  110.   if DebugWin = 0 then
  111.     DebugWin := StartDebugWin;
  112.  
  113.   if DebugWin <> 0 then
  114.   begin
  115.     MessageString := MsgPrefix + Msg;
  116.     CDS.cbData := Length(MessageString) + 4;
  117.     CDS.dwData := 0;
  118.     if Msg = chrClearCommand then
  119.       CDS.lpData := PChar(chrClearCommand+Char(Ord(MType) + 1)+ MessageString +#0)
  120.     else
  121.       CDS.lpData := PChar(#1+Char(Ord(MType) + 1)+ MessageString +#0);
  122.     SendMessage(DebugWin, WM_COPYDATA, WParam(Application.Handle), LParam(@CDS));
  123.   end;
  124. {$ENDIF not LINUX}
  125. end;
  126.  
  127. procedure SendDebug(const Msg: string);
  128. begin
  129.   SendDebugEx(Msg, mtInformation);
  130. end;
  131.  
  132. procedure SendDebugFmt(const Msg: string; const Args: array of const);
  133. begin
  134.   SendDebugEx(Format(Msg, Args), mtInformation);
  135. end;
  136.  
  137. procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TMsgDlgType);
  138. begin
  139.   SendDebugEx(Format(Msg, Args), MType);
  140. end;
  141.  
  142. procedure SendDebugClear;
  143. begin
  144.   SendDebug(chrClearCommand);
  145. end;
  146.  
  147. const
  148.   Indentation = '    ';
  149.  
  150. procedure SendMethodEnter(const MethodName: string);
  151. begin
  152.   MsgPrefix := MsgPrefix + Indentation;
  153.   SendDebugEx('Entering ' + MethodName, mtInformation);
  154. end;
  155.  
  156. procedure SendMethodExit(const MethodName: string);
  157. begin
  158.   SendDebugEx('Exiting ' + MethodName, mtInformation);
  159.  
  160.   Delete(MsgPrefix, 1, Length(Indentation));
  161. end;
  162.  
  163. procedure SendSeparator;
  164. const
  165.   SeparatorString = '------------------------------';
  166. begin
  167.   SendDebugEx(SeparatorString, mtInformation);
  168. end;
  169.  
  170. procedure SendBoolean(const Identifier: string; const Value: Boolean);
  171. begin
  172.   // Note: We deliberately leave "True" and "False" as
  173.   // hard-coded string constants, since these are
  174.   // technical terminology which should not be localised.
  175.   if Value then
  176.     SendDebugEx(Identifier + '= True', mtInformation)
  177.   else
  178.     SendDebugEx(Identifier + '= False', mtInformation);
  179. end;
  180.  
  181. procedure SendInteger(const Identifier: string; const Value: Integer);
  182. begin
  183.   SendDebugEx(Format('%s = %d', [Identifier, Value]), mtInformation);
  184. end;
  185.  
  186. procedure SendDateTime(const Identifier: string; const Value: TDateTime);
  187. begin
  188.   SendDebugEx(Identifier + '=' + DateTimeToStr(Value), mtInformation);
  189. end;
  190.  
  191. end.
  192.  
  193.